package uk.ac.aber.Blockmation;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import uk.ac.aber.Blockmation.FileFilters.*;

/**
 * This class provides FileChooser for our application,
 * allowing to browse through directories, saving and loading 
 * single frames or whole projects.
 */
public class OurFileChooser extends JFileChooser
{
    /*
     * FileFilters set file formats which user can choose from.
     * Prevents from using file formats which our application cannot read.
     */
    private ImageFileFilter imageFileFilter;
    private MovieFileFilter movieFileFilter;
    private BmpSavingFilter bmpSavingFilter;
    private GifSavingFilter gifSavingFilter;
    private JpgSavingFilter jpgSavingFilter;
    private PngSavingFilter pngSavingFilter;
    private TxtSavingFilter txtSavingFilter;
    
    private JFrame parent;

    /**
     * Constructs the file chooser with its file filters.
     * @param parent - JFrame Window to open dialogs window onto
     */
    public OurFileChooser(JFrame parent)
    {
        imageFileFilter = new ImageFileFilter();
        movieFileFilter = new MovieFileFilter();
        bmpSavingFilter= new BmpSavingFilter();
        gifSavingFilter=new GifSavingFilter();
        jpgSavingFilter=new JpgSavingFilter();
        pngSavingFilter=new PngSavingFilter();
        txtSavingFilter=new TxtSavingFilter();
        this.parent = parent;
    }
    
    /**
     * For importing single images.Opens dialog window and enables to load a
     * file with extension GIF, BMP, JPG or PNG.
     */
    public File importImageIntoFrame()
    {
        this.setDialogTitle("Import Image Into Frame");
        this.resetChoosableFileFilters();

        this.addChoosableFileFilter(imageFileFilter);

        File importedImageFile = null;

        int returnVal = this.showOpenDialog(parent);
        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            importedImageFile = this.getSelectedFile();
        }
       
        return importedImageFile;
    }

    /**
     * For saving single frame. Opens dialog window and enables to save frame 
     * as a file with extension GIF, BMP, JPG or PNG. Adds chosen file extension if
     * file hasn't got one. 
     */
    public File exportFrameAsImage()
    {
    	
    	this.setDialogTitle("Export Frame as Image");
    	this.resetChoosableFileFilters();

    	this.setAcceptAllFileFilterUsed(false);

    	this.addChoosableFileFilter(bmpSavingFilter);
    	this.addChoosableFileFilter(jpgSavingFilter);
    	this.addChoosableFileFilter(pngSavingFilter);
    	this.addChoosableFileFilter(gifSavingFilter);

        File savedImageFile = showSaveDialogCheckOverwriting();

        return savedImageFile;
    }

    /**
     * For saving whole project(movie). Opens dialog window and enables to save 
     * project as txt or gif file.
     */
    public File saveProjectAs()
    {
        
    	this.setDialogTitle("Save Projest As");
    	this.resetChoosableFileFilters();
    	this.setAcceptAllFileFilterUsed(false);

        // delete the below and add separate filters
    	this.addChoosableFileFilter(txtSavingFilter);
    	this.addChoosableFileFilter(gifSavingFilter);
        
        // here also you can choose only ONE file format to save in

    	File savedProjectFile = showSaveDialogCheckOverwriting();

        return savedProjectFile;
    }

    /**
     * Checks if file with chosen name already exists and asks user if 
     * he/she wants to overwrite it.
     */
    private File showSaveDialogCheckOverwriting() {
		
        File savingFile = null;
        boolean keepShowing = true;

        while (keepShowing){

            int returnVal = this.showSaveDialog(parent);

            if (returnVal == JFileChooser.APPROVE_OPTION)
            {
            	savingFile = this.getSelectedFile();
                
            	FileFilterExtension currentFilterExtension = (FileFilterExtension) this.getFileFilter();
            	String extension = currentFilterExtension.getExtension();

            	
                if ( (savingFile.getName().toLowerCase().endsWith(extension)==false)  )
                {
                	savingFile = new File(savingFile.getAbsolutePath()+"."+extension);
                }
                
                if (savingFile.exists())
                {
                    int n = JOptionPane.showConfirmDialog(this,
                            "The provided file exists. Do you want to override it?", "File exists",
                            JOptionPane.YES_NO_OPTION);

                    
                    if (n==JOptionPane.NO_OPTION){
                    	keepShowing = true;
                    }
                    else if (n == JOptionPane.YES_OPTION)
                    {
                    	
                    	keepShowing = false;
                    }
                }
                else 
                {
                	keepShowing = false;
                }
            } else 
            {
            	keepShowing = false;
            }
            
        }
       
        return savingFile;
		
	}

    /**
     * To load a whole project from a file. Opens a dialog window and enables 
     * to load project from txt or gif file.
     */
    public File loadProject()
    {
        
    	this.setDialogTitle("Load Project");
    	this.resetChoosableFileFilters();
    	this.setAcceptAllFileFilterUsed(false);
    	this.addChoosableFileFilter(movieFileFilter);

        File loadedProjectFile = null;

        int returnVal = this.showOpenDialog(parent);

        if (returnVal == JFileChooser.APPROVE_OPTION)
        {
            loadedProjectFile = this.getSelectedFile();
        }

        return loadedProjectFile;
    }
    
}
